package com.qingke.memo.command.memo;

import java.util.List;

import com.qingke.memo.command.MemoCommand;
import com.qingke.memo.pojo.CustomTag;
import com.qingke.memo.pojo.Memo;
import com.qingke.memo.pojo.MemoCustomTag;
import com.qingke.memo.unit.MConsole;

public class AlterMemoCustomTag extends MemoCommand {

	public AlterMemoCustomTag(Memo memo) {
		super(memo);
	}

	@Override
	public void execute() {
		List<CustomTag> list=dao.selectCustomTagsByMemo(memo);
		if(list.size()==0){
			MConsole.println("the memo have no custom tag.");
			return ;
		}
		MConsole.println("-------custom tags-------");
		for(CustomTag ct:list){
			MConsole.println(ct);
		}
		MConsole.println();
		
		int oldId=MConsole.getInt("old custom tag id:");
		if(!dao.isCustomTagExistById(oldId)){
			MConsole.println("the custom tag is not existed.");
			return ;
		}
		CustomTag oldCustomTag=dao.selectCustomTagById(oldId);
		if(!list.contains(oldCustomTag)){
			MConsole.println("It is not custom tag of the memo.");
			return ;
		}
		
		int newId=MConsole.getInt("new custom tag id:");
		if(!dao.isCustomTagExistById(newId)){
			MConsole.println("the custom tag is not existed.");
			return ;
		}
		CustomTag newCustomTag=dao.selectCustomTagById(newId);
		if(list.contains(newCustomTag)){
			MConsole.println("It has been custom tag of the memo.");
			return ;
		}
		
		MemoCustomTag memoCustomTag=new MemoCustomTag();
		memoCustomTag.setMemoId(memo.getId());
		memoCustomTag.setCustomTagId(oldId);
		
		memoCustomTag=dao.selectMemoCustomTagByMemoAndCustomTag(memoCustomTag);
		memoCustomTag.setCustomTagId(newId);
		
		boolean isSuccess=dao.updateMemoCustomTag(memoCustomTag);
		if(isSuccess)
			MConsole.println("alter memo custom tag success.");
		else
			MConsole.println("alter memo custom tag failed.");
		
	}

}
